package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.adapter.SimpleBatchAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.bean.SimpleBatchBean;
import com.txsj.scan.presenter.SimpleBatchPresenter;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.utils.glide.GlideImgManager;
import com.txsj.scan.view.IBatchView;
import com.txsj.scan.widget.DragFloatActionButton;
import com.txsj.scan.widget.TitleBarView;
import com.txsj.scan.widget.swipemenu.SwipeRecyView;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by wangchuangjun on 2018/11/21
 * Desc: 选完商品后选择商品绑定批次
 */

public class SimpleGoodsBatchActivity extends BaseActivity<SimpleBatchPresenter> implements
        IBatchView {

    public static final int RESULT_REFRESH_CODE = 0x116;
    DecimalFormat decimalFormat = new DecimalFormat("0.00");

    TitleBarView mTitle;
    ImageView iv_goods_pic;
    TextView tv_goods_name, tv_goods_stockCount,tv_price;
    SwipeRecyView recyclerView;
    SimpleBatchAdapter adapter;

    DragFloatActionButton fab;
    XRefreshView xRefreshView;
    GoodsInfo goodsInfo;
    int pageNo = 1;
    SimpleBatchBean simpleBatchBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_batch);
        initView();
        initData();
    }

    private void initData() {
        if (null == getIntent() || null == getIntent().getStringExtra("data")) return;
        goodsInfo = new Gson().fromJson(getIntent().getStringExtra("data"), GoodsInfo.class);

        GlideImgManager.getLoadImg(this,goodsInfo.getGoods_img(),iv_goods_pic);
        tv_goods_name.setText(goodsInfo.getGoods_name());
        tv_price.setText("￥"+decimalFormat.format(goodsInfo.getGoods_price()));
        tv_goods_stockCount.setText(goodsInfo.getGs1_num()+"");

        //只有当前仓库才能进行批次盘点，其他商品只能新增批次
        presenter.getBatchHttpData(goodsInfo.getGs1_num(), pageNo);
    }

    private void initView() {
        mTitle = (TitleBarView) findViewById(R.id.mTitle);
        iv_goods_pic = (ImageView) findViewById(R.id.iv_goods_pic);
        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_goods_name = (TextView) findViewById(R.id.tv_goods_name);
        tv_goods_stockCount = (TextView) findViewById(R.id.tv_goods_stockCount);
        fab = (DragFloatActionButton) findViewById(R.id.fab);

        xRefreshView = (XRefreshView) findViewById(R.id.xRefreshView);
        recyclerView = (SwipeRecyView) findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter = new SimpleBatchAdapter(this);
        recyclerView.setAdapter(adapter);
        initEmptyView();
        xRefreshView.enableEmptyView(false);
        xRefreshView.setPullRefreshEnable(false);
        xRefreshView.setAutoLoadMore(false);
        xRefreshView.startRefresh();
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                pageNo = 1;
                presenter.getBatchHttpData(goodsInfo.getGs1_num(), pageNo);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                presenter.getBatchHttpData(goodsInfo.getGs1_num(), pageNo);
            }
        });

        adapter.setListener(new SimpleBatchAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                if (adapter.get(position).isSelect()) {
                    simpleBatchBean = adapter.get(position);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    public void clickConfirm(View view) {
        Intent intent = getIntent();
        if (null == simpleBatchBean)simpleBatchBean = adapter.get(0);//刚进入界面默认选择第一个不点击
        intent.putExtra("data", simpleBatchBean);
        setResult(LabelActivity.RESULT_GOODS_BATCH_CODE,intent);
        finish();
    }


    public void clickCancel(View view) {
        finish();
    }

    @Override
    protected SimpleBatchPresenter providePresenter() {
        return new SimpleBatchPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    @Override
    public void onSuccess(List<SimpleBatchBean> list) {
        hideLoading();
        xRefreshView.stopLoadMore();
        if (null == list || list.size() == 0) {
            if (pageNo == 1) {
                onError("暂无数据");
            } else {
                onLoadMoreError("加载失败");
            }
        } else {
            if (pageNo == 1) {
                xRefreshView.stopRefresh();
            } else {
                xRefreshView.stopLoadMore();
            }

        }
        adapter.setList(list,pageNo);
        pageNo++;
    }


    @Override
    public void onLoading() {
        if (pageNo == 1) {
            showLoading();
        }
    }

    @Override
    public void onError(String msg) {
        hideLoading();
        showErrorMsg(msg);
        xRefreshView.enableEmptyView(true);
        xRefreshView.stopRefresh();
    }

    @Override
    public void onLoadEnd(String msg) {
        xRefreshView.stopLoadMore();
        hideLoading();
    }

    @Override
    public void onLoadMoreError(String msg) {
        xRefreshView.stopLoadMore();
        hideLoading();
    }

    Button btn_empty_reload;

    /**
     * 列表数据为空时 显示的View
     */
    private void initEmptyView() {
        View emptyView = View.inflate(this, R.layout.view_empty, null);
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRefreshView.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xRefreshView.startRefresh();
            }
        });
    }

}
